﻿using System.ComponentModel;
using VNet.VisiSuite.Common.Resource;

namespace VNet.VisiSuite.VisiAdmin.Models
{
    public class ProductModel : ComparableBaseModel<ProductModel>, IDataErrorInfo
    {
        #region Fields
        private string _sku;
        private string _description;
        private string _revision;
        private int _part_time;
        private int _setup_time;
        private int _cycle_time;
        private int _loadunload_time;
        private bool _isEnabled;
        private string _lastUpdatedByUser;
        private string _lastUpdatedDate;
        private int _productId;
        #endregion

        #region Properties

        public int ProductId
        {
            get { return _productId; }
            set
            {
                if (_productId != value)
                {
                    _productId = value;
                    OnPropertyChanged("ProductId");
                }
            }
        }

        [Category("Properties")]
        [DisplayName("Sku")]
        [Description("Product Sku.")]
        [DefaultValue("")]
        public string Sku
        {
            get { return _sku; }
            set
            {
                if (_sku != value)
                {
                    _sku = value;
                    OnPropertyChanged("Sku", true);
                }
            }
        }

        [Category("Properties")]
        [DisplayName("Description")]
        [Description("Product Description.")]
        [DefaultValue("")]
        public string Description
        {
            get { return _description; }
            set
            {
                if (_description != value)
                {
                    _description = value;
                    OnPropertyChanged("Description", true);
                }
            }
        }

        [Category("Properties")]
        [DisplayName("Revision")]
        [Description("Product Revision.")]
        [DefaultValue("")]
        public string Revision
        {
            get { return _revision; }
            set
            {
                if (_revision != value)
                {
                    _revision = value;
                    OnPropertyChanged("Revision", true);
                }
            }
        }

        [Category("Properties")]
        [DisplayName("Part Time ̣̣̣̣̣(sec)")]
        [Description("Product part time (sec).")]
        public int PartTime
        {
            get { return _part_time; }
            set
            {
                if (_part_time != value)
                {
                    _part_time = value;
                    OnPropertyChanged("PartTime", true);
                }
            }
        }

        [Category("Properties")]
        [DisplayName("Enabled")]
        [Description("Enable / disable the product.")]
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                OnPropertyChanged("IsEnabled", true);
            }
        }

        [Category("Properties")]
        [DisplayName("Setup Time (sec)")]
        [Description("Product Setup (sec).")]
        public int SetupTime
        {
            get { return _setup_time; }
            set
            {
                _setup_time = value;
                OnPropertyChanged("SetupTime", true);
            }
        }

        [Category("Properties")]
        [DisplayName("Cycle Time (sec)")]
        [Description("Product cycle time (sec).")]
        public int CycleTime
        {
            get { return _cycle_time; }
            set
            {
                _cycle_time = value;
                OnPropertyChanged("CycleTime", true);
            }
        }

        [Category("Properties")]
        [DisplayName("Load / UnloadTime (sec)")]
        [Description("Product load / unload time (sec).")]
        public int LoadUnloadTime
        {
            get { return _loadunload_time; }
            set
            {
                _loadunload_time = value;
                OnPropertyChanged("LoadUnloadTime", true);
            }
        }

        [Category("Update History")]
        [DisplayName("Created By")]
        [Description("User who created the product in VisiAdmin.")]
        [ReadOnly(true)]
        public string CreatedByUser { get; set; }

        [Category("Update History")]
        [DisplayName("Created On")]
        [Description("Date when the product was created in VisiAdmin.")]
        [ReadOnly(true)]
        public string CreatedDate { get; set; }

        [Category("Update History")]
        [DisplayName("Updated By")]
        [Description("User who last edited the product in VisiAdmin.")]
        [ReadOnly(true)]
        public string LastUpdatedByUser
        {
            get { return _lastUpdatedByUser; }
            set
            {
                _lastUpdatedByUser = value;
                OnPropertyChanged("LastUpdatedByUser");
            }
        }

        [Category("Update History")]
        [DisplayName("Updated On")]
        [Description("Date when the product was last edited in VisiAdmin.")]
        [ReadOnly(true)]
        public string LastUpdatedDate
        {
            get { return _lastUpdatedDate; }
            set
            {
                _lastUpdatedDate = value;
                OnPropertyChanged("LastUpdatedDate");
            }
        }

        public string Enabled
        {
            get { return IsEnabled == false ? "Disabled" : "Enabled"; }
        }

        #endregion

        #region IDataErrorInfo Implementation

        public string Error
        {
            get { return string.Empty; }
        }

        public string this[string columnName]
        {
            get
            {
                if (!ShouldValidate)
                    return string.Empty;

                if (columnName == "Sku" && string.IsNullOrWhiteSpace(Sku))
                    return ValidationMessages.ProductNameRequired;
                
                return string.Empty;
            }
        }

        #endregion

        #region Override Equals methods
        public bool Equals(ProductModel other)
        {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;

            return string.Equals(Sku, other.Sku)
                && Description == other.Description
                && Revision == other.Revision
                && PartTime == other.PartTime
                && SetupTime == other.SetupTime
                && CycleTime == other.CycleTime
                && LoadUnloadTime == other.LoadUnloadTime
                && IsEnabled == other.IsEnabled;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (obj.GetType() != GetType())
                return false;
            return Equals((ProductModel)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var result = 0;
                result = (result * 397) ^ (Sku ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (Description ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (Revision ?? string.Empty).GetHashCode();
                result = (result * 397) ^ PartTime.GetHashCode();
                result = (result * 397) ^ SetupTime.GetHashCode();
                result = (result * 397) ^ CycleTime.GetHashCode();
                result = (result * 397) ^ LoadUnloadTime.GetHashCode();
                result = (result * 397) ^ IsEnabled.GetHashCode();
                return result;
            }
        }
        #endregion
    }
}
